package com.zhuoyue.biz.core.command.impl;

import com.zhuoyue.biz.core.api.domain.TblDeviceActions;
import com.zhuoyue.biz.core.command.constant.DuoCaiDeng0Faa0B02CmdDeal;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.CmdUtils;
import com.zhuoyue.common.utils.JsonUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghui
 */
public class DanSeWen0Faa0D03 {
    /**
     * 总亮度实际3000
     */
    private static final Integer TOTAL_BRIGHTNESS = 30;
    private static boolean IS_GROUP_ALL = false;
    private static final String ACTIONS = "actions";
    private static final Integer ONE_HUNDRED = 100;
    /**
     * 区间
     */
    private static final String DURATION = "duration";

    public static String getCommand(Map<String, Object> map, String gatewayMac, String deviceMac, List<TblDeviceActions> deviceActions, boolean isGroup) throws ParseException {
        IS_GROUP_ALL = isGroup;
        String controlType = (String) map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }
        Integer delayTimeInt = 0;
        Integer setColorTemperatureStr = 1500;
        Integer setBrightnessStr = 1500;
        if (CollectionUtils.isNotEmpty(deviceActions)) {
            for (TblDeviceActions action : deviceActions) {
                if ("setBrightness".equals(action.getActionsKey())) {
                    setBrightnessStr = Integer.parseInt(action.getActionsValue());
                }
                if ("setColorTemperature".equals(action.getActionsKey())) {
                    setColorTemperatureStr = Integer.parseInt(action.getActionsValue());
                }
            }
        }
        Map<String, Object> actions = null;
        String duration = "0000";
        String delayTime = "0000";
        Integer setColorTemperature = setColorTemperatureStr;
        Integer setBrightness = setBrightnessStr;
        if (map.get(ACTIONS) != null) {
            actions = JsonUtils.jsonToMap(map.get("actions").toString());

            if (actions.get(DURATION) == null) {
                throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "actions中duration参数不能为空");
            }

            duration = CmdUtils.getHighPositionReplacement(CmdUtils.getHexString(Integer.parseInt(actions.get("duration") + "")));

            delayTime = actions.get("delayTime") != null ? CmdUtils.getHighPositionReplacement(CmdUtils.getHexString(Integer.parseInt(actions.get("delayTime") + "")))
                    : CmdUtils.getHighPositionReplacement(CmdUtils.getHexString((delayTimeInt)));

            setColorTemperature = actions.get("setColorTemperature") != null ? Integer.parseInt(actions.get("setColorTemperature") + "") : setColorTemperatureStr;
            if (setColorTemperature == 0) {
                setColorTemperature = 1;
            }
            setBrightness = actions.get("setBrightness") != null ? Integer.parseInt(actions.get("setBrightness") + "") : setBrightnessStr;
            if (setBrightness == 0) {
                setBrightness = 1;
            }
        }


        String cmd = "";
        switch (controlType) {
            //开灯
            case ControlType.TURN_ON_REQUEST:
                cmd = setOpenControlCmd(gatewayMac, deviceMac, duration, delayTime, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //关灯
            case "TurnOffRequest":
                cmd = setCloseControlCmd(gatewayMac, deviceMac, duration, delayTime, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //控制色温+亮度
            case "SetBrightnessAndColorTemperaturePercentageRequest":
                cmd = setBrightnessAndColorTemperaturePercentage(dealBrightness(setBrightness), dealColorTemperature(setColorTemperature), duration, delayTime, gatewayMac, deviceMac);
                break;
            //只设置灯亮度
            case "SetBrightnessPercentageRequest":
                cmd = setBrightnessPercentage(dealBrightness(setBrightness), duration, delayTime, gatewayMac, deviceMac);
                break;
            //只设置色温
            case "SetColorTemperaturePercentageRequest":
                cmd = setColorTemperaturePercentage(dealBrightness(setColorTemperature), duration, delayTime, gatewayMac, deviceMac);
                break;
            //设置状态(三种状态,冷 暖 灭)
            case "SetStatusRequest":
                cmd = setStatus(dealBrightness(setBrightness), dealColorTemperature(setColorTemperature), gatewayMac, deviceMac);
                break;
            //立即亮一段时间再灭
            case "OnToOffRequest":
                cmd = setOnToOffOrOfftoOn("07", duration, delayTime, gatewayMac, deviceMac, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //立即灭一段时间再亮
            case "OffToOnRequest":
                cmd = setOnToOffOrOfftoOn("08", duration, delayTime, gatewayMac, deviceMac, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //取消延时
            case "CancelDelayRequest":
                cmd = setCancelDelay(gatewayMac, deviceMac, duration, delayTime, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
            default:
                break;
        }
        return cmd;
    }

    public static String setCancelDelay(String gatewayMac, String deviceMac, String duration, String delayTime, String nowBrightnessStr, String nowColorTemperatureStr) {
        String cmd10To12 = "1F000000" + "0D" + duration + delayTime + "00000000" + nowBrightnessStr + nowColorTemperatureStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }


    /**
     * 打开
     *
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setOpenControlCmd(String gatewayMac, String deviceMac, String duration, String delayTime, String nowBrightnessStr, String nowColorTemperatureStr) {
        String cmd10To12 = "1F000000" + "01" + duration + delayTime + "00000000" + nowBrightnessStr + nowColorTemperatureStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }


    /**
     * 获取关灯控制命令
     *
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setCloseControlCmd(String gatewayMac, String deviceMac, String duration, String delayTime, String nowBrightnessStr, String nowColorTemperatureStr) {
        String cmd10To12 = "1F000000" + "02" + duration + delayTime + "00000000" + nowBrightnessStr + nowColorTemperatureStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }


    /**
     * 色温+亮度
     *
     * @param dealBrightness
     * @param colorTemperature
     * @return
     */
    private static String setBrightnessAndColorTemperaturePercentage(String dealBrightness, String colorTemperature, String duration, String delayTime, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "03" + duration + delayTime + "00000000" + dealBrightness + colorTemperature;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    /**
     * 亮度
     *
     * @param brightnessInt
     * @return
     */
    private static String setBrightnessPercentage(String brightnessInt, String duration, String delayTime, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "04" + duration + delayTime + "00000000" + brightnessInt + "0000";
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    /**
     * 色温
     *
     * @param colorTemperature
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setColorTemperaturePercentage(String colorTemperature, String duration, String delayTime, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "05" + duration + delayTime + "000000000000" + colorTemperature;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    private static String setStatus(String dealBrightness, String colorTemperature, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "06" + "0000000000000000" + dealBrightness + colorTemperature;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    /**
     * 立即亮一段时间再灭
     * 立即灭一段时间再亮
     *
     * @param duration   亮度保持时间
     * @param delayTime  延时时间
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setOnToOffOrOfftoOn(String mode, String duration, String delayTime, String gatewayMac, String deviceMac, String nowColorTemperatureStr, String nowBrightnessStr) {
        String cmd10To12 = "1F000000" + mode + duration + delayTime + "00000000" + nowColorTemperatureStr + nowBrightnessStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    private static String cmdData(String cmd10To12, String gatewayMac, String deviceMac) {
        String cmd9 = CmdUtils.getDateLength(cmd10To12);
        String cmd4 = "";
        if (IS_GROUP_ALL) {
            cmd4 = "02";
        } else {
            cmd4 = "01";
        }
        String cmd3To12 = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_3 +
                cmd4 +
                gatewayMac + deviceMac +
                DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_7 +
                DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_8 +
                cmd9 + cmd10To12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_1 +
                cmd2 + cmd3To12 + cmd13 +
                DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_14;
        return fullCmd;
    }


    private static String dealBrightness(Integer doubleBrightness) {
        int one = 2;
        int two = 2;
        int three = 2;
        if (doubleBrightness > ONE_HUNDRED) {
            doubleBrightness = 100;
        }
        String setBrightness = "0";
        if (doubleBrightness == one) {
            setBrightness = "6";
        } else if (doubleBrightness == two) {
            setBrightness = "20";
        } else if (doubleBrightness == three) {
            setBrightness = "60";
        } else {
            //计算亮度
            setBrightness = TOTAL_BRIGHTNESS * doubleBrightness + "";
        }

        int brightnessInt = Integer.parseInt(setBrightness);
        String br1 = CmdUtils.getHexString(brightnessInt);
        String sub = CmdUtils.getHighPositionReplacement(br1);
        return sub;
    }

    private static String dealColorTemperature(Integer setColorTemperature) {
        if (setColorTemperature > ONE_HUNDRED) {
            setColorTemperature = 100;
        }
        //计算色温
        Integer colorTemperatureInt = TOTAL_BRIGHTNESS * setColorTemperature;
        String br1 = CmdUtils.getHexString(colorTemperatureInt);
        String sub = CmdUtils.getHighPositionReplacement(br1);
        return sub;
    }
}
